import {
  Body,
  Controller,
  Get,
  HttpCode,
  HttpStatus,
  Param,
  Post,
  Put,
  Query,
  UseGuards,
} from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { CurUser } from 'src/decorators/user.decorator';
import { AuthGuard } from '../auth/auth.guard';
import { UserEntity } from '../user/entities/user.entity';
import { CommentService } from './comment.service';
import { CreateCommentDto } from './dto/create.comment.dto';
@ApiTags('后台管理系统-评论管理')
@UseGuards(AuthGuard)
@Controller('comment')
export class CommentController {
  constructor(private readonly commentService: CommentService) {}

  @ApiOperation({ summary: '发表评论（文章评论或者留言）' })
  @HttpCode(200)
  @Post(':source')
  async comment(
    @Body() createCommentDto: CreateCommentDto,
    @Param('source') source: string,
    @CurUser() user: UserEntity,
  ) {
    // 判断是不是留言，或者文章评论的，不是报错
    if (!this.commentService.isValidSource(source)) {
      return '报错';
    }
    console.log('2022-10-13 15:27:40', createCommentDto);
    // 创建评论
    const comment = await this.commentService.create(source, createCommentDto, user.id);
    // this.commentService.censor(source, String(comment.id));
    // this.commentService.notifyComment(source, createCommentDto, user.id);
    return comment;
  }

  @ApiOperation({ summary: '获取第一级评论' })
  @HttpCode(HttpStatus.OK)
  @Get('list/:source/:id')
  async getCommentList(@Param('source') source: string, @Param('id') id: string) {
    return await this.commentService.getCommentList(source, id);
  }

  @ApiOperation({ summary: '获取子级评论' })
  @HttpCode(HttpStatus.OK)
  @Get('list/:source')
  async getChildCommentList(@Param('source') source: string, @Query('rootId') rootId: string) {
    return await this.commentService.getChildCommentList(source, rootId);
  }

  @ApiOperation({ summary: '给评论点赞' })
  @HttpCode(HttpStatus.OK)
  @Put('like/:source/:id')
  async clickLike(
    @Param('source') source: string,
    @Param('id') id: string,
    @CurUser() user: UserEntity,
  ) {
    return await this.commentService.clickLike(source, id, user);
  }
}
